package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"os"
	"spider/handle"
	"strings"
	"utils/cont"
	"utils/tools/httpUtils"
)

/*
//https://store.steampowered.com/api/appdetails/?appids=1279610
//游戏价格
//https://store.steampowered.com/api/packagedetails/?packageids=479270&cc=CN
*/

func asd() {
	url := "https://store.steampowered.com/about/"
	client := httpUtils.CreatHttpClient("http://172.24.127.113:7890")

	if body, b := handle.RequestBody(client, url); b {
		doc := handle.Doc(body)
		result := handle.Handle2(doc)
		marshal, err := cont.Json.Marshal(result)

		if err != nil {
			return
		}
		fmt.Println(string(marshal))

		os.WriteFile("/home/debi/IdeaProjects/EmpShop/Server/spider/main.json", marshal, 777)

	}
}

type Sasd struct {
	Title, Img1, Img2, Des string
}

func main() {
	//url := "https://steamdb.info/charts/"
	//client := httpUtils.CreatHttpClient("http://172.24.127.113:7892")

	file, err := os.ReadFile("/home/debi/IdeaProjects/EmpShop/Server/spider/s.html")
	if err != nil {
		return
	}

	doc := handle.Doc(file)
	handle2 := Handle2(doc)
	marshal, err := cont.Json.Marshal(handle2)
	if err != nil {
		return
	}
	fmt.Println(string(marshal))

	os.WriteFile("/home/debi/IdeaProjects/EmpShop/Server/spider/main.json", marshal, 777)

	//os.WriteFile("/home/debi/IdeaProjects/EmpShop/Server/spider/main.json", marshal, 777)

}

func Handle2(doc *goquery.Document) []string {

	var ss []string

	// Find the review items
	doc.Find(".promoted-articles-item__title").Each(func(i int, selection *goquery.Selection) {

		val := selection.Text()
		val = strings.ReplaceAll(val, "\n", "")
		val = strings.ReplaceAll(val, "\t", "")
		ss = append(ss, val)
	})

	return ss
}
